package com.trucle.vntraffic;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

//import com.google.android.maps.GeoPoint;
//import com.google.android.maps.MapView;
//import com.google.android.maps.MapActivity;

public class OutFindPath extends ListActivity {
	
	private String jsonUrl = "http://maps.googleapis.com/maps/api/directions/json?";
	 
	ArrayList<HashMap<String, String>> insList = new ArrayList<HashMap<String, String>>();
	String[] aList = new String []{"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""};
	 	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.output_find_path);
        
        StringBuffer temptext = new StringBuffer("Only show one of ways in Demo version");
        HashMap<String, String> map = new HashMap<String, String>();
        int tempL=1;
        
        Intent k = getIntent();
        // Receiving the Data
        String source = k.getStringExtra("source");
        String dest = k.getStringExtra("dest");
        int goBy = k.getIntExtra("goby", 1);
                
        StringBuffer sBuf = new StringBuffer(jsonUrl);
        sBuf.append("origin=");
        sBuf.append(source);
        sBuf.append("&destination=");
        sBuf.append(dest);
        sBuf.append("&sensor=true&units=metric");
        
        if ( goBy == 0 )
        {
        	sBuf.append("&mode=walking");
        }
        else if (goBy == 1)
        {
        	sBuf.append("&mode=driving");
        }
        else if (goBy == 2)
        {
        	sBuf.append("&mode=bicycling");
        }
        
        
        JSONParser jParser = new JSONParser();
        try {
        	JSONObject json = jParser.getJSONFromUrl(sBuf.toString(),0);
        	
            //Get the route object
        	Log.v("trucle"," parse to string = "+json.toString());
            final JSONObject jsonRoute = json.getJSONArray("routes").getJSONObject(0);
            
            //Log.v("trucle","log = "+jsonRoute.toString());
             
            //Get the leg, only one leg as we don't support waypoints
            final JSONObject leg = jsonRoute.getJSONArray("legs").getJSONObject(0);
            
            //Get the steps for this leg
            final JSONArray steps = leg.getJSONArray("steps");
            
            
            String leng = leg.getJSONObject("distance").getString("text");
            String duration = leg.getJSONObject("duration").getString("text");
            temptext.append(" - khoang cach ");
            temptext.append(leng);
            temptext.append(" m - ");
            temptext.append("thoi gian di ");
            temptext.append(duration);
            temptext.append(" s");
            
            aList[0]=temptext.toString();
            

            //Number of steps for use in for loop
            final int numSteps = steps.length();
            
            /*
            //Set the name of this route using the start & end addresses
            route.setName(leg.getString("start_address") + " to " + leg.getString("end_address"));
            //Get google's copyright notice (tos requirement)
            route.setCopyright(jsonRoute.getString("copyrights"));
            //Get the total length of the route.
            route.setLength(leg.getJSONObject("distance").getInt("value"));
            //Get any warnings provided (tos requirement)
            if (!jsonRoute.getJSONArray("warnings").isNull(0)) {
                    route.setWarning(jsonRoute.getJSONArray("warnings").getString(0));
            }
            */
            
            /* Loop through the steps, creating a segment for each one and
             * decoding any polylines found as we go to add to the route object's
             * map array. Using an explicit for loop because it is faster!
             */
            
            for (int i = 0; i < numSteps; i++) {
            	
            		StringBuffer lBuf = new StringBuffer("Đi ");
            	
                    //Get the individual step
                    final JSONObject step = steps.getJSONObject(i);
                    //Get the start position for this step and set it on the segment
                    
                    //Set the length of this segment in metres
                    String length = step.getJSONObject("distance").getString("text");
                    String dur = step.getJSONObject("duration").getString("text");
                    //dur = dur/60;
                    String ins = step.getString("html_instructions").replaceAll("<(.*?)*>", "");
                    Log.v("trucle","instruction "+ins);
                    
                    lBuf.append(length);
                    lBuf.append(" khoảng ");
                    lBuf.append(dur);
                    lBuf.append(" - Lộ trình: ");
                    lBuf.append(ins);
                                        
                    //segment.setLength(length);
                    //segment.setDistance(distance/1000);
                    //Strip html from google directions and set as turn instruction
                    //segment.setInstruction(step.getString("html_instructions").replaceAll("<(.*?)*>", ""));
                    //Retrieve & decode this segment's polyline and add it to the route.
                    //route.addPoints(decodePolyLine(step.getJSONObject("polyline").getString("points")));
                    //Push a copy of the segment to the route
                    //route.addSegment(segment.copy());
                    //map.put("ins", lBuf.toString());
                    //insList.add(map);
                    
                    aList[tempL] =  lBuf.toString();
            		tempL++;
                    }
            
            } 
        catch (JSONException e) {
    	e.printStackTrace();
    	}
        /*
        for (HashMap.Entry<String,String> entry : map.entrySet()) {
        	{
        		aList[tempL] = entry.getValue();
        		tempL++;
        	}
        }
        */
        setListAdapter(new ArrayAdapter<String>(this, R.layout.output_find_path,aList));

		ListView listView = getListView();
		listView.setTextFilterEnabled(true);
        /*
        setListAdapter(new ArrayAdapter<String>(this, R.layout.output_find_path,aList));

		ListView listView = getListView();
		listView.setTextFilterEnabled(true);
        */
        //end of void
        }
	
}
